package se.webbzon.boltzmann.camera;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import se.webbzon.oschi01.worldarea.WorldArea;
import se.webbzon.oschi01.worldeditor.DeflatableWorldObject;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class FixedCamera extends AbstractCamera implements TargetableCamera, MovableCamera, DeflatableWorldObject {
	
	// The name of the deflatable world object
	public static final String worldObjectName = "fixedCamera";
	
	// The platform and target of the camera
	private WorldStaticObject platform, target;
	
	// The camera position and target
	private final Point3d from, to;
	
	// The up vector of the camera
	private final Vector3d up;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public FixedCamera() {
		this(null,null);
	}

	public FixedCamera(WorldStaticObject platform, WorldStaticObject target) {
		super(WorldObject.onStep);
		this.platform = platform;
		this.target = target;
		from = new Point3d();
		to = new Point3d();
		up = new Vector3d(0,1,0);
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the platform of this fixed camera. **/
	public WorldStaticObject getPlatform() {
		return platform;
	}
	
	/** Returns the target of this fixed camera. **/
	public WorldStaticObject getTarget() {
		return target;
	}
	
	@Override public void setPlatform(WorldStaticObject platform) {
		this.platform = platform;
	}

	@Override public void setTarget(WorldStaticObject target) {
		this.target = target;
	}
	
	@Override public void onStep(WorldArea world) {
		// boolean moved = false;
		if (platform != null && platform.distanceTo(from) > 0) {
			platform.getPosition(from);
			// moved = true;
		}
		if (target != null && target.distanceTo(to) > 0) {
			target.getPosition(to);
			// moved = true;
		}
		//if (moved)
			setProjectionPerspective(from, to, up);
	}

	@Override public String getName() {
		return worldObjectName;
	}

	@Override public WorldObjectDeflation getDeflation() {
		return new FixedCameraDeflation(0,this);
	}
	
}
